import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.Component;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.GridLayout;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JPanel;


public class ResultForm {

	public JFrame frame;
	private JTable table;
	private JPanel panel;
	private JLabel lblPriority;
	private JLabel lblWish;
	private JLabel labelPrioValue;
	private JLabel labelWishValue;
	private int[] schedule;
	private List<Tutor> tutors;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ResultForm window = new ResultForm();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ResultForm() {
		Exemplar bestExemplar = Adapter.getBestExemplar();
		schedule = new int[Adapter.LECTURE_COUNT];
		System.arraycopy(bestExemplar.getSchedule(), 0, schedule, 0, Adapter.LECTURE_COUNT);
		tutors = Adapter.getTutors();
		
		initialize();
		fillTable();
	}

	@SuppressWarnings("serial")
	private void fillTable() {		
		for (int i = 0; i < Adapter.LECTURE_COUNT; i++) {
            table.setValueAt(tutors.get(schedule[i]).getName(), i % 5, i / 5 + 1);
        }
		
		table.setDefaultRenderer(table.getColumnClass(2), new DefaultTableCellRenderer(){
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {   
		        super.setHorizontalAlignment(SwingConstants.CENTER);
		        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		        if (column != 0) {
			        int idx = (column - 1) * 5 + row;
			        if (tutors.get(schedule[idx]).getWishes()[idx]) {
			        	//super.setBackground(new Color(0, 125, 0));
			        	super.setBackground(Color.green);
			        } else {
			        	//super.setBackground(new Color(125, 0, 0));
			        	super.setBackground(Color.red);
			        }
		        } else {
		        	super.setBackground(Color.white);
		        }
				return this;   
			}
		});
	}
	
	private void updatePrioWish() {
		if (table.getSelectedColumn() != 0) {
	    	int idx = (table.getSelectedColumn() - 1) * 5 + table.getSelectedRow();
	    	
	    	labelPrioValue.setText(Double.toString(tutors.get(schedule[idx]).getPriority()));
	    	labelWishValue.setText(Boolean.toString(tutors.get(schedule[idx]).getWishes()[idx]));
		}
	}
	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings("serial")
	private void initialize() {
		frame = new JFrame();
		frame.setTitle(Integer.toString(Adapter.getGenerationSize()));
		frame.setBounds(100, 100, 498, 218);
		frame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("482px:grow"),},
			new RowSpec[] {
				RowSpec.decode("113px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("bottom:max(40dlu;default):grow"),}));
		
		table = new JTable();
		JScrollPane jscrlp = new JScrollPane(table);
		frame.getContentPane().add(jscrlp, "1, 1, fill, top");
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(false);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{"1", null, null, null, null, null, null},
				{"2", null, null, null, null, null, null},
				{"3", null, null, null, null, null, null},
				{"4", null, null, null, null, null, null},
				{"5", null, null, null, null, null, null},
			},
			new String[] {
				"\u2116", "\u041F\u043D", "\u0412\u0442", "\u0421\u0440", "\u0427\u0442", "\u041F\u0442", "\u0421\u0431"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class, Object.class, Object.class, Object.class, Object.class, Object.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		
		panel = new JPanel();
		frame.getContentPane().add(panel, "1, 3, fill, fill");
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		lblPriority = new JLabel("\u041F\u0440\u0438\u043E\u0440\u0438\u0442\u0435\u0442:");
		panel.add(lblPriority, "2, 2");
		
		labelPrioValue = new JLabel("");
		panel.add(labelPrioValue, "4, 2");
		
		lblWish = new JLabel("\u041F\u043E\u0436\u0435\u043B\u0430\u043D\u0438\u0435:");
		panel.add(lblWish, "2, 4");
		
		labelWishValue = new JLabel("");
		panel.add(labelWishValue, "4, 4");
		table.getColumnModel().getColumn(0).setPreferredWidth(32);
		
		table.getColumnModel().getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent e) {
                        if (e.getValueIsAdjusting() && table.getSelectedRow() != -1 && table.getSelectedColumn() != -1) {
                        	updatePrioWish();
                        }
                    }
                });
		
		table.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                	@Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (e.getValueIsAdjusting() && table.getSelectedRow() != -1 && table.getSelectedColumn() != -1) {
                        	updatePrioWish();
                        }   
                    }
                });

	}

}
